package dev.alm.reggietakeoutservice.utils;

import dev.alm.reggietakeoutservice.entity.User;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

// JWT工具类
public class JwtUtil {

    private static final String secret = "reggie";

    public static String createToken(User user) {
        Long id = user.getId();
        String username = user.getUsername();
        JwtBuilder jwtBuilder = Jwts.builder().setSubject(username).setIssuedAt(new Date()).signWith(SignatureAlgorithm.HS512, JwtUtil.secret);
        return jwtBuilder.compact();
    }

    public static Claims parseJwt(String token) {
        Claims claims = Jwts.parser().setSigningKey(JwtUtil.secret).parseClaimsJws(token).getBody();
        return claims;
    }
}
